package com.wangxinrou.wangxinrou_surprisemeal.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wangxinrou.wangxinrou_surprisemeal.mapper.FriendMapper;
import com.wangxinrou.wangxinrou_surprisemeal.model.Friend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FriendService {
    @Autowired
    private FriendMapper friendMapper;

    // 获取用户的所有好友
    public List<Friend> getFriendsByUserId(int userId) {
        return friendMapper.selectList(new QueryWrapper<Friend>().eq("user_id", userId));
    }

    // 添加好友
    public boolean addFriend(Friend friend) {
        // 在添加之前，可以进行一些逻辑检查，如是否已经是好友等
        return friendMapper.insert(friend) > 0;
    }

    // 删除好友关系
    public boolean removeFriend(int userId, int friendId) {
        return friendMapper.delete(new QueryWrapper<Friend>()
                .eq("user_id", userId)
                .eq("friend_id", friendId)) > 0;
    }

    // 检查是否为好友
    public boolean isFriend(int userId, int friendId) {
        Friend friend = friendMapper.selectOne(new QueryWrapper<Friend>()
                .eq("user_id", userId)
                .eq("friend_id", friendId));
        return friend != null;
    }

    // 获取好友数量
    public int getFriendCount(int userId) {
        return friendMapper.selectCount(new QueryWrapper<Friend>().eq("user_id", userId)).intValue();
    }
}
